import LXCOM from '../common/common.js'
import baseunitgrah from './baseunitgrah.js'
import { CoordTRFUtil } from '../common/tools/coordtool.js'
import Coord from '../common/struct/coord.js'
import actionPkg from '../graph/action/polygonaction.js'
import utilfuncs from '../common/tools/utilfuncs.js'
var funcs = utilfuncs.UtilFuncs
import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import {FillUtil} from '../graph/paintutil/fillutil.js'
import {Strokeutil} from '../graph/paintutil/strokeutil.js'

import {LGXEnums}  from '../common/sysenum.js'

import polygon from '../common/struct/basestruct/polygon.js'
import { GlobalVar } from '../common/globalvar.js'

class PolygonGraph extends baseunitgrah.BaseUnitGraph {
    constructor(type) {
        super(type);
        var p = new Coord.MultiPos();
        this.setPos(p);

        var action = new actionPkg.PolygonGraphAction(this);
        this.setActionSvc(action);
        this.m_graphType = LGXEnums.LGXGraphType.PolygonGraph_type;
    }

    display(map, mapInfo) {
        var scaling = mapInfo.getLenthDisScale();
        map.beginPath();
        var ptlst = new Array();
        var position = this.getPos();
        var lenth = position.posList.length;
        var pt = position.posList[0];
        var p0 = CoordTRFUtil.world2Screen(pt, mapInfo);
        map.moveTo(p0.x, p0.y);
        ptlst.push(p0);
        for (var i = 1; i < lenth; i++) {
            pt = position.posList[i];
            var p = CoordTRFUtil.world2Screen(pt, mapInfo);
            map.lineTo(p.x, p.y);
            ptlst.push(p);
        }
        map.lineTo(p0.x, p0.y);
        ptlst.push(p0);

        map.closePath();
        Strokeutil.active(this._lineDef, this, map, mapInfo);
        FillUtil.active(this._fillDef, map, this, mapInfo, false);

        super.displayGraphScope(map, mapInfo);
        this.displayFeaturePoint(map, ptlst, mapInfo);
    }

    disInSymbol(transformInfo,map, mapInfo,forClip) {
        var scaling = mapInfo.getLenthDisScale();

        map.beginPath();
        map.lineWidth = this._lineDef.lineWidth;
        map.strokeStyle = "#ee00cc";

        var position = this.getPos();
        var lenth = position.posList.length;
        var pt = position.posList[0];
        var x = pt.x * scaling;
        var y = -pt.y * scaling;
        var x0 = x;
        var y0 = y;
        map.moveTo(x, y);
        for (var i = 1; i < lenth; i++) {
            pt = position.posList[i];
            x = pt.x * scaling;
            y = -pt.y * scaling;
            map.lineTo(x, y);
        }
        map.lineTo(x0, y0);
        map.closePath();
        if (!forClip){
            Strokeutil.active(this._lineDef, this, map, mapInfo);
            FillUtil.active(this._fillDef, map, this, mapInfo, true);
        }
    }

    calScope(mapscale) {
        var position = this.getPos();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return;
        }
        let rect = position.getZone();
        this._zone.clone(rect);
    }

    getLocalZone() {
        return super.getMultiPointGraphLocalZone();
    }

    distance(tPos, scaling, mapscale) {
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return -1;
        }
        if (!super.preCheckInScope(tPos, scaling)) {
            return -1;
        }

        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        //多边形头尾是相连的
        var pPrePos = position.posList[0];
        let posList = position.posList;
        posList.push(pPrePos);

        let minDis = -1;
        for (var i = 0; i < posList.length - 1; i++) {
            let pCntPos = posList[i + 1];
            let dis = funcs.distanceLineSec4Visual(tPos, pPrePos, pCntPos, scaling);
            if (minDis < 0) {
                minDis = dis;
            }
            else if (dis >= 0 && minDis > dis){
                minDis = dis;
            }
            pPrePos = pCntPos;
        }

        if (!GlobalVar.easySelMode) {
            if (minDis < cmpGap && minDis >= 0) {
                return minDis;
            }
            else{
                return -1;
            }
        }
        else{
            if (this._fillDef.fillStyle != LGXEnums.LGXFillStyle.EMPTY){
                return 0;
            }
            else{
                return minDis;
            }
        }
    }

    displayFeaturePoint(map, ptLst, mapInfo) {
        if (!this._selected) {
            return;
        }
        super.displayFeaturePoint(map, mapInfo);
        var lenth = ptLst.length;
        for (var i = 0; i < lenth; i++) {
            var point = ptLst[i];
            super.drawFocusPoint(map, point.x, point.y, "#ffff00");
        }
    }

    calculateSubKind(tPoint, scaling, mapscale) {
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return 0;
        }
        if (!super.preCheckInScope(tPoint, scaling)) {
            return 0;
        }
        var seleKind = 0;
        var tAbsorbPt = new LXCOM.LGXPoint();
        tAbsorbPt.clone(tPoint);

        //多边形头尾是相连的
        var pPrePos = position.posList[0];
        let posList = position.posList;
        posList.push(pPrePos);

        let minDis = 100;
        var cmpGap = super.getSelCmpGap(scaling, mapscale);      
        for (var i = 1; i < posList.length; i++) {
            let pCntPos = posList[i];
            let dis = funcs.distanceLineSec4Visual(tPoint, pPrePos, pCntPos, scaling);
            if (dis <= cmpGap && dis >= 0 && dis <= minDis) {
                minDis = dis;
                tAbsorbPt.clone(tPoint);
                var nConnType = funcs.adsorbPosOnLineSec(tAbsorbPt, pPrePos, pCntPos, scaling);
                if (nConnType % 10 == 0) {
                    seleKind = lenth + 1;
                }
                else {
                    var n = Math.floor(nConnType / 10);
                    seleKind = n + i;
                }
            }
            pPrePos = pCntPos;
        }

        if (GlobalVar.easySelMode) {
            seleKind = LGXEnums.LGXModGraphKind.Drag_center;
        }

        if (seleKind > 0) {
            tPoint.clone(tAbsorbPt);
        }

        return seleKind;
    }

    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }

        var position = this.getPos();
        var lenth = position.posList.length;
        if (this._selKind > lenth) {
            this.translate(tOffset, modCmdInfo.mapscale,false);
        }
        else if (this._selKind <= lenth && this._selKind > 0) {
            let pPos = position.posList[this._selKind - 1];
            pPos.x += tOffset.x;
            pPos.y += tOffset.y;
        }
    }

    getMainFeaturePTLst(tFeaturePTLst) {
        var position = this.getPosCopy();
        let p0 = position.posList[0];
        position.addPoint(p0);

        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pt = position.posList[i];
            tFeaturePTLst.push({pt,kind:LGXEnums.AuxPointKind.end});
        }

        let prePt = position.posList[0];
        for (let i = 1; i < lenth; i++) {
            let pt = position.posList[i];
            let x = (prePt.x + pt.x)/2;
            let y = (prePt.y + pt.y)/2;
            let t = {x,y};
            tFeaturePTLst.push({pt:t,kind:LGXEnums.AuxPointKind.end});
            prePt = pt;
        }
    }

    getSubFeaturePTLst(tFeaturePTLst,mousePoint) {
        var position = this.getPosCopy();
        let p0 = position.posList[0];
        position.addPoint(p0);

        var lenth = position.posList.length;
        let tMayAbsorbPtLst = [];
        let prePt = position.posList[0];
        for (let i = 1; i < lenth; i++) {
            let nextPt = position.posList[i];
            let vector = funcs.getVector(prePt, nextPt);
            let projPt = funcs.getPointProj2Line(mousePoint, prePt, vector);
            if (projPt && funcs.chkPointOnLineSec(projPt,prePt,nextPt)) {
                let dis = funcs.distancePoint(projPt, mousePoint);
                let t = { dis, pt: projPt };
                tMayAbsorbPtLst.push(t);
            }
            prePt = nextPt;
        }

        //排序
        let sortedList = tMayAbsorbPtLst.sort(function (p1, p2) {
            if ((p1.dis - p2.dis) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );
        if  (sortedList.length){
            let t = sortedList[0];
            tFeaturePTLst.push({ pt: t.pt, kind: LGXEnums.AuxPointKind.commonPt });
        }
    }

    assignInfo(info) {
        super.assignInfo(info);
    }

    modifyAttribute(info, mapscale) {
        super.modifyAttribute(info,mapscale);
        this.calScope(mapscale);
    }

    toInfo() {
        var info = new polygon.Polygon();
        this.toInfo4InheritClass(info);
        info.strPosition = this.getPos().toString();
        return info;
    }

    getPath() {
        let path = [];
        var position = this.getPos();
        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pt = position.posList[i];
            path.push({ x: pt.x, y: pt.y });
        }
        return path;
    }

    modifyCoord(propName, propVal, mapscale) {
        var position = this.getPos();
        if (propName == "poslist") {
            var lenth = position.posList.length;
            for (var i = 0; i < lenth; i++) {
                var pt = position.posList[i];
                let jsonObj = propVal[i];
                pt.x = parseFloat(jsonObj.x);
                pt.y = parseFloat(jsonObj.y);
            }
        }
        this.notifyToObserver();
        this.calScope(mapscale);
    }

    getCoordInfo() {
        let graphPropDef = this.getCoordPropDef();
        let tableData = [];
        let arr = [];
        var position = this.getPos();
        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pt = position.posList[i];
            arr.push({ x: pt.x, y: pt.y });
        }

        let polistObj = {
            propName: 'poslist',
            propLabel: '顶点列表',
            propValue: arr
        };
        tableData.push(polistObj);

        let tableForm = {
            'tableData': tableData
        };
        return { graphPropDef, tableForm };
    }

    getCoordPropDef() {
        let graphPropDef = {
            poslist: {
                vtype: global.PtList_CoordType,
                range: { min: 1, max: 10 },
            }
        }
        return graphPropDef;
    }

    getPosCopy() {
        let pos = new Coord.MultiPos();
        pos.clone(this._position);
        return pos;
    }

    getDimDatumFeatureLst() {
        let rstArr = [];

        let idkey = 1;
        //图形中心
        {
            let cnt = this.getZone().getCnt();
            let data = {x:cnt.x,y:cnt.y};
            let info = {type:LGXEnums.DimDatumType.center,data,id:idkey++ };
            rstArr.push(info);
        }

        let position = this.getPos();

        //顶点特征
        let lenth = position.posList.length;
        for (let i = 0; i < lenth; i++) {
            let pt = position.posList[i];
            let data = {idx:i,x:pt.x,y:pt.y}
            let info = {type:LGXEnums.DimDatumType.vertex,data,id:idkey++ };
            rstArr.push(info);
        }

        //线中心特征
        let prePt = position.posList[0];
        for (var i = 1; i < lenth; i++) {
            let cntPt = position.posList[i];
            let x = (prePt.x + cntPt.x)/2;
            let y = (prePt.y + cntPt.y)/2;
            let data = {stIdx:i - 1,endIdx:i,x,y};
            let info = {type:LGXEnums.DimDatumType.midpt,data,id:idkey++ };
            rstArr.push(info);
            prePt = cntPt;
        }

        //尾和头顶点
        {
            let cntPt = position.posList[0];
            let x = (prePt.x + cntPt.x)/2;
            let y = (prePt.y + cntPt.y)/2;
            let data = {stIdx:lenth - 1,endIdx:0,x,y};
            let info = {type:LGXEnums.DimDatumType.midpt,data,id:idkey++ };
            rstArr.push(info);
        }

        //每条线段特征
        prePt = position.posList[0];
        for (var i = 1; i < lenth; i++) {
            let cntPt = position.posList[i];
            let st = {x:prePt.x,y:prePt.y};
            let end = {x:cntPt.x,y:cntPt.y};
            let data = {stIdx:i - 1,endIdx:i,st,end};
            let info = {type:LGXEnums.DimDatumType.edge,data,id:idkey++ };
            rstArr.push(info);
            prePt = cntPt;
        }
        //尾和头顶点
        {
            let cntPt = position.posList[0];
            let st = {x:prePt.x,y:prePt.y};
            let end = {x:cntPt.x,y:cntPt.y};
            let data = {stIdx:lenth - 1,endIdx:0,st,end};
            let info = {type:LGXEnums.DimDatumType.edge,data,id:idkey++ };
            rstArr.push(info);
        }

        return rstArr;
    }

    onChgDimValue(dimGraph,oldValue,newValue){
        console.log("polygon graph onChgDimValue******");
        let svc = new PolylineDimChg(this);
        svc.onChgDimValue(dimGraph,oldValue,newValue);
    }

    onDimBaseChged(dimGraph){
        console.log("polygon onDimBaseChged******");
        let svc = new PolylineDimBaseChange(this);
        svc.onBaseChanged(dimGraph);
    }

    getLocalOrigin(){
        let pt = {x:0,y:0}
        let position = this.getPos();
        let p0 = position.posList[0];
        pt.x = p0.x;
        pt.y = p0.y;
        return pt;
    }

    clipFillZone_ex(map,mapInfo,transformInfo){
        map.clip();
        map.beginPath();
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        var pt = position.posList[0];
        var p0 = CoordTRFUtil.world2Screen(pt, mapInfo);

        map.moveTo(p0.x, p0.y);
        for (var i = 1; i < lenth; i++) {
            pt = position.posList[i];
            var p = CoordTRFUtil.world2Screen(pt, mapInfo);
            map.lineTo(p.x, p.y);
        }
        map.lineTo(p0.x, p0.y);
        map.closePath();
        return 1;
    }

    //检测point点是否落在当前图形对象的边界上
    chPtOnEdge(point,scaling){
        return funcs.chkPtOnPolyline(point,this._position.posList,global.SelectGap,scaling)
    }
}


export default { PolygonGraph }
